package comment

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	commentRequest "github.com/flipped-aurora/gin-vue-admin/server/model/comment/request"
	commonRequest "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	commonResponse "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service/comment"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CommentApi struct {
	CommentCService     comment.CommentCService
	CommentAdminService comment.CommentAdminService
}

// GetByFuzzy 根据内容,模糊查询,admin
func (c *CommentApi) GetByFuzzy(ctx *gin.Context) {
	uri := "GetByFuzzy"
	var logID = utils.GetLogID(ctx)
	var page commonRequest.PageInfo

	if binErr := ctx.ShouldBind(&page); binErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(binErr))
		commonResponse.FailWithMessage(binErr.Error(), ctx)
		return
	}

	pageResult, listErr := c.CommentAdminService.GetByFuzzy(ctx, page)
	if listErr != nil {
		global.GVA_LOG.Error("模糊查询评论失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.String("keyword", page.Keyword),
			zap.Error(listErr))
		commonResponse.FailWithMessage(listErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("模糊查询评论成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.String("keyword", page.Keyword),
		zap.Int64("total", pageResult.Total))
	commonResponse.OkWithData(pageResult, ctx)
}

// GetByUserName 查询该用户下的id,admin
func (c *CommentApi) GetByUserName(ctx *gin.Context) {
	uri := "GetByUserName"
	var logID = utils.GetLogID(ctx)
	var pageInfo commentRequest.GetCommentByUsername

	if binErr := ctx.ShouldBind(&pageInfo); binErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(binErr))
		commonResponse.FailWithMessage(binErr.Error(), ctx)
		return
	}

	pageRes, GetUserErr := c.CommentAdminService.GetByName(ctx, pageInfo)
	if GetUserErr != nil {
		global.GVA_LOG.Error("按用户名查询评论失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.String("user_name", pageInfo.UserName),
			zap.Error(GetUserErr))
		commonResponse.FailWithMessage(GetUserErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("按用户名查询评论成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.String("user_name", pageInfo.UserName),
		zap.Int64("total", pageRes.Total))
	commonResponse.OkWithData(pageRes, ctx)
}

// GetByAuthorityId 通过comment_id查询,admin
func (c *CommentApi) GetByAuthorityId(ctx *gin.Context) {
	uri := "GetByAuthorityId"
	var logID = utils.GetLogID(ctx)
	var commentId commentRequest.GetCommentById

	if binErr := ctx.ShouldBind(&commentId); binErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(binErr))
		commonResponse.FailWithMessage(binErr.Error(), ctx)
		return
	}

	commentRes, GetUsersErr := c.CommentAdminService.GetCommentById(ctx, commentId)
	if GetUsersErr != nil {
		global.GVA_LOG.Error("通过评论ID查询评论失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("comment_id", commentId.CommentId),
			zap.Error(GetUsersErr))
		commonResponse.FailWithMessage(GetUsersErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("通过评论ID查询评论成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("comment_id", commentId.CommentId))
	commonResponse.OkWithData(commentRes, ctx)
}

// DeleteComment 删除评论及其子评论,admin
func (c *CommentApi) DeleteComment(ctx *gin.Context) {
	uri := "DeleteComment"
	var logID = utils.GetLogID(ctx)
	var req commentRequest.DeleteAdminCommentId

	if binErr := ctx.ShouldBind(&req); binErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(binErr))
		commonResponse.FailWithMessage(binErr.Error(), ctx)
		return
	}

	DeleteErr := c.CommentAdminService.DeleteComment(ctx, req)
	if DeleteErr != nil {
		global.GVA_LOG.Error("管理员删除评论失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("comment_id", req.CommentId),
			zap.Error(DeleteErr))
		commonResponse.FailWithMessage(DeleteErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("管理员删除评论成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("comment_id", req.CommentId))
	commonResponse.OkWithMessage("删除成功", ctx)
}

// DetailComment 查看评论详情,c端
func (c *CommentApi) DetailComment(ctx *gin.Context) {
	uri := "DetailComment"
	var logID = utils.GetLogID(ctx)
	var detail commentRequest.GetCommentById

	if binErr := ctx.ShouldBind(&detail); binErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(binErr))
		commonResponse.FailWithMessage(binErr.Error(), ctx)
		return
	}

	detailComment, DetailErr := c.CommentAdminService.GetCommentById(ctx, detail)
	if DetailErr != nil {
		global.GVA_LOG.Error("查看评论详情失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("comment_id", detail.CommentId),
			zap.Error(DetailErr))
		commonResponse.FailWithMessage(DetailErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("查看评论详情成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("comment_id", detail.CommentId))
	commonResponse.OkWithData(detailComment, ctx)
}

// ClickCount 统计点击数量,c端
func (c *CommentApi) ClickCount(ctx *gin.Context) {
	uri := "ClickCount"
	var logID = utils.GetLogID(ctx)
	var clickCount commentRequest.ClickCount

	if binErr := ctx.ShouldBind(&clickCount); binErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(binErr))
		commonResponse.FailWithMessage(binErr.Error(), ctx)
		return
	}

	operate, clickCountErr := c.CommentCService.ClickCount(ctx, clickCount)
	if clickCountErr != nil {
		global.GVA_LOG.Error("评论互动操作失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("comment_id", clickCount.CommentId),
			zap.Int64("op_type", clickCount.OpType),
			zap.Error(clickCountErr))
		commonResponse.FailWithMessage(clickCountErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("评论互动操作成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("comment_id", clickCount.CommentId),
		zap.String("operate", operate))
	commonResponse.OkWithMessage("操作成功", ctx)
}

// DeleteCComment 删除自己的评论,c端
func (c *CommentApi) DeleteCComment(ctx *gin.Context) {
	uri := "DeleteCComment"
	var logID = utils.GetLogID(ctx)
	var req commentRequest.DeleteCCommentId

	if binErr := ctx.ShouldBind(&req); binErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(binErr))
		commonResponse.FailWithMessage(binErr.Error(), ctx)
		return
	}

	DeleteErr := c.CommentCService.DeleteComment(ctx, req)
	if DeleteErr != nil {
		global.GVA_LOG.Error("用户删除评论失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("user_id", req.UserId),
			zap.Int64("comment_id", req.CommentId),
			zap.Error(DeleteErr))
		commonResponse.FailWithMessage(DeleteErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("用户删除评论成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("user_id", req.UserId),
		zap.Int64("comment_id", req.CommentId))
	commonResponse.OkWithMessage("删除成功", ctx)
}

// AddComment 发布评论
func (c *CommentApi) AddComment(ctx *gin.Context) {
	uri := "AddComment"
	var logID = utils.GetLogID(ctx)
	var request commentRequest.AddCommentRequest

	if bindErr := ctx.ShouldBind(&request); bindErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(bindErr))
		commonResponse.FailWithMessage(bindErr.Error(), ctx)
		return
	}

	if addErr := c.CommentCService.AddComment(ctx, request); addErr != nil {
		global.GVA_LOG.Error("发布评论失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("content_id", request.ContentID),
			zap.Int64("publisher_id", request.PublisherID),
			zap.Error(addErr))
		commonResponse.FailWithMessage(addErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("发布评论成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("content_id", request.ContentID),
		zap.Int64("publisher_id", request.PublisherID))
	commonResponse.OkWithMessage("success", ctx)
}

// UpdateComment 修改评论
func (c *CommentApi) UpdateComment(ctx *gin.Context) {
	uri := "UpdateComment"
	var logID = utils.GetLogID(ctx)
	var request commentRequest.UpdateCommentRequest

	if bindErr := ctx.ShouldBind(&request); bindErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(bindErr))
		commonResponse.FailWithMessage(bindErr.Error(), ctx)
		return
	}

	if addErr := c.CommentCService.UpdateComment(ctx, request); addErr != nil {
		global.GVA_LOG.Error("修改评论失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("comment_id", request.ID),
			zap.Error(addErr))
		commonResponse.FailWithMessage(addErr.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("修改评论成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("comment_id", request.ID))
	commonResponse.OkWithMessage("success", ctx)
}

// GetCommentList c端查询评论列表
func (c *CommentApi) GetCommentList(ctx *gin.Context) {
	uri := "GetCommentList"
	var logID = utils.GetLogID(ctx)
	var request commentRequest.QueryCommentCRequest

	if bindErr := ctx.ShouldBind(&request); bindErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(bindErr))
		commonResponse.FailWithMessage(bindErr.Error(), ctx)
		return
	}

	commentList, errGet := c.CommentCService.GetCommentList(ctx, request)
	if errGet != nil {
		global.GVA_LOG.Error("查询评论列表失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("content_id", request.ContentID),
			zap.Int64("parent_comment_id", request.ParentCommentID),
			zap.Error(errGet))
		commonResponse.FailWithMessage(errGet.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("查询评论列表成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("content_id", request.ContentID),
		zap.Int64("parent_comment_id", request.ParentCommentID),
		zap.Int64("total", commentList.Total))
	commonResponse.OkWithData(commentList, ctx)
}

// GetCommentUserAction 查询用户评论操作记录
func (c *CommentApi) GetCommentUserAction(ctx *gin.Context) {
	uri := "GetCommentUserAction"
	var logID = utils.GetLogID(ctx)
	var request commentRequest.CommentUserActionRequest

	if bindErr := ctx.ShouldBindQuery(&request); bindErr != nil {
		global.GVA_LOG.Error("参数绑定失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Error(bindErr))
		commonResponse.FailWithMessage(bindErr.Error(), ctx)
		return
	}

	result, err := c.CommentCService.GetCommentUserAction(ctx, request)
	if err != nil {
		global.GVA_LOG.Error("查询用户评论操作记录失败!",
			zap.String("uri", uri),
			zap.String("log_id", logID),
			zap.Int64("user_id", request.UserID),
			zap.Int64("action_type", int64(request.ActionType)),
			zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), ctx)
		return
	}

	global.GVA_LOG.Info("查询用户评论操作记录成功",
		zap.String("uri", uri),
		zap.String("log_id", logID),
		zap.Int64("user_id", request.UserID),
		zap.Int64("action_type", int64(request.ActionType)),
		zap.Int64("total", result.Total))
	commonResponse.OkWithData(result, ctx)
}
